package com.mystnihon.tdroid.manager.ftp;

import java.io.IOException;
import java.net.SocketException;
import java.util.EventListener;
import java.util.Observable;

import my.swing.event.EventListenerLists;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import android.content.Context;
import android.os.AsyncTask;

public class FTPManager extends Observable implements FTPInterface {

	public static final int Connected = 0, ConnectionFailed = 1, AuthentificationSucceed = 2,
			AuthentificationFailed = 3, DownloadSuccess = 4, DownloadFailed = 5, FileList = 6, NotDisconnected = 7,
			Disconnected = 8;

	private boolean mIsConnected = false;
	private EventListenerLists mListeners;
	private FTPClient mFtpClient;

	public FTPManager(Context context) {
		mListeners = new EventListenerLists();
		mFtpClient = new FTPClient();
	}

	public void connect(final String host, final int port) {

		if (!mIsConnected) {
			new AsyncTask<Void, Void, Boolean>() {

				@Override
				protected Boolean doInBackground(Void... params) {

					try {
						mFtpClient.connect(host, port);
						return true;
					} catch (SocketException e) {
						e.printStackTrace();
						return false;
					} catch (IOException e) {
						e.printStackTrace();
						return false;
					}

				}

				protected void onPostExecute(Boolean result) {
					super.onPostExecute(result);
					fireConnect(result.booleanValue());
				};

			}.execute();
		} else {
			fireConnect(true);
		}
	}

	public void disconnect() {
		new AsyncTask<Void, Void, Void>() {
			@Override
			protected Void doInBackground(Void... params) {
				try {
					mFtpClient.disconnect();
				} catch (IOException e) {
				}
				return null;
			}

			protected void onPostExecute(Void result) {
				super.onPostExecute(result);
				fireDisconnect();

			};
		}.execute();
	}

	@Override
	public void download(FTPFile file) {
		// TODO Auto-generated method stub

	}

	public boolean isConnected() {
		return mFtpClient.isConnected();
	}

	@Override
	public void listDirectories() {
		try {
			FTPFile[] file = mFtpClient.listDirectories();
			fireDirectory(file, null);
		} catch (IOException e) {
			// TODO Auto-generated catch block

		}

	}

	@Override
	public void listDirectories(String directory) {
		try {
			FTPFile[] file = mFtpClient.listFiles(directory);
			fireDirectory(file, directory);
		} catch (IOException e) {
			// TODO Auto-generated catch block

		}

	}

	@Override
	public void listFiles() {
		try {
			FTPFile[] file = mFtpClient.listDirectories();
			fireFiles(file, null);
		} catch (IOException e) {
			// TODO Auto-generated catch block

		}

	}

	@Override
	public void listFiles(String directory) {
		try {
			FTPFile[] file = mFtpClient.listFiles(directory);
			fireFiles(file, directory);
		} catch (IOException e) {
			// TODO Auto-generated catch block

		}

	}

	@Override
	public void login(final String login, final String password) {
		new AsyncTask<String, Void, Boolean>() {
			@Override
			protected Boolean doInBackground(String... params) {
				try {
					return mFtpClient.login(login, password);
				} catch (IOException e) {
					return false;
				}

			}

			protected void onPostExecute(Boolean result) {
				super.onPostExecute(result);
				fireAuthenticated(result.booleanValue());
			};
		}.execute(login, password);
	}

	public void setFTPListener(FTPListener listener) {
		mListeners.add(FTPListener.class, listener);
	}

	private void fireAuthenticated(boolean logged) {
		for (FTPListener listener : getFTPListeners()) {
			listener.onAuthenticated(logged);
		}
	}

	private void fireConnect(boolean connected) {
		for (FTPListener listener : getFTPListeners()) {
			listener.onConnect(connected);
		}
	}

	private void fireDirectory(FTPFile[] files, String directory) {
		for (FTPListener listener : getFTPListeners()) {
			listener.onDirectories(files, directory);
		}
	}

	private void fireDisconnect() {
		for (FTPListener listener : getFTPListeners()) {
			listener.onDisconnected();
		}
	}

	private void fireFiles(FTPFile[] files, String directory) {
		for (FTPListener listener : getFTPListeners()) {
			listener.onFiles(files, directory);
		}
	}

	private FTPListener[] getFTPListeners() {
		return mListeners.getListeners(FTPListener.class);
	}

	public static interface FTPListener extends EventListener {
		public void onAuthenticated(boolean success);

		public void onConnect(boolean connected);

		public void onDirectories(FTPFile[] files, String directory);

		public void onDisconnected();

		public void onDownloadError(int errorCode, String downloadFailed, int downloadPos);

		public void onDownloadFinished(int downloadPos);

		public void onFiles(FTPFile[] files, String directory);
	}

	public void clearListeners() {
		mListeners.clearListeners();
	}

}
